package com.qf.JDBC;

import java.sql.*;

/**
 * @author wangyu
 * @Classname: DBUtitls
 * @Description: TEST
 * @date 2022-04-04 14:24
 */
public class DBUtitls {
//    类加载一次
    static {
        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    //    1链接
    public static Connection getConnection() {
        Connection connection = null;//修改后
        try {

            String url = "jdbc:mysql://localhost:3306/dbtest1?useUnicode=true&characterEncoding=utf8";
            String root = "root";
            String pwd = "12345";
            connection = DriverManager.getConnection(url, root, pwd);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return connection;
    }

    //    2释放资源
    public static void closeAll(Connection connection, Statement statement, ResultSet resultSet) {
        try {
            if (resultSet != null) {
                resultSet.close();
            }
            if (statement != null) {
                statement.close();
            }
            if (connection != null) {
                connection.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
